package castlewarserver;

import gameObjects.Castle;
import gameObjects.Unit;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;

import upgrades.Upgrade;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.StaxDriver;

public class XmlSerializer  {

	public static synchronized void serializeUnits( ArrayList<Unit> units,String file){
		XStream xstream = new XStream(new StaxDriver());
		xstream.alias("unit", Unit.class);
		
		
		FileOutputStream fo=null;
		try{
			fo=new FileOutputStream(file);
			xstream.toXML(units, fo);
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		
	}
	
	public static synchronized void serializeUpgrades( ArrayList<Upgrade> upgrades,String file){
		XStream xstream = new XStream(new StaxDriver());
		xstream.alias("upgrade", Upgrade.class);
		
		
		FileOutputStream fo=null;
		try{
			fo=new FileOutputStream(file);
			xstream.toXML(upgrades, fo);
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		
	}
	
	public static synchronized void serializeCastle (Castle castle,String fileSrc){
		XStream xstream = new XStream(new StaxDriver());
		xstream.alias("castle", Castle.class);
		
		
		FileOutputStream fo=null;
		try{
			fo=new FileOutputStream(fileSrc);
			xstream.toXML(castle, fo);
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	public static synchronized void serializeGameParams (GameParams params,String fileSrc){
		XStream xstream = new XStream(new StaxDriver());
		xstream.alias("gameparams", GameParams.class);
		
		
		FileOutputStream fo=null;
		try{
			fo=new FileOutputStream(fileSrc);
			xstream.toXML(params, fo);
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	public static synchronized ArrayList<Unit> deserializeUnits(String file){
		XStream xstream =new XStream(new StaxDriver());
		xstream.alias("unit", Unit.class);
		ArrayList<Unit> units= null;
		try{
			FileInputStream fi= new FileInputStream(file);
			units=(ArrayList<Unit>)xstream.fromXML(fi);
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		return units;
	}
	
	public static synchronized Castle deserializeCastle(String file){
		XStream xstream = new XStream(new StaxDriver());
		xstream.alias("castle", Castle.class);
		Castle castle= null;
		try{
			FileInputStream fi= new FileInputStream(file);
			castle=(Castle)xstream.fromXML(fi);
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		return castle;
	}
	
	public static synchronized GameParams deserializeGameParams(String file){
		XStream xstream = new XStream(new StaxDriver());
		xstream.alias("gameparams", GameParams.class);
		GameParams params= null;
		try{
			FileInputStream fi= new FileInputStream(file);
			params=(GameParams)xstream.fromXML(fi);
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		return params;
	}
	public static synchronized ArrayList<Upgrade> deserializeUpgrades(String file){
		XStream xstream =new XStream(new StaxDriver());
		xstream.alias("upgrade", Upgrade.class);
		ArrayList<Upgrade> upgrades= null;
		try{
			FileInputStream fi= new FileInputStream(file);
			upgrades=(ArrayList<Upgrade>)xstream.fromXML(fi);
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		return upgrades;
	}
	
}
